package com.migu.contacts.main.my;

import static com.migu.contacts.net.Service.API;

import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.BottomPopup;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.BuildConfig;
import com.migu.contacts.R;
import com.migu.contacts.bean.PayRequest;
import com.migu.contacts.bean.PayResponse;
import com.migu.contacts.bean.VipBean;
import com.migu.contacts.databinding.ActivityVipactivityBinding;
import com.migu.contacts.databinding.ItemVipBinding;
import com.migu.contacts.databinding.ItemVipPayBinding;
import com.migu.contacts.databinding.PopVipBinding;
import com.migu.contacts.util.AliPayUtils;
import com.migu.contacts.util.AvatarUtil;
import com.migu.contacts.util.UserCenter;

import java.util.ArrayList;
import java.util.List;

public class VIPActivity extends BaseActivity<ActivityVipactivityBinding> {
    @Override
    protected void initData() {
        super.initData();
        API.getProduct().watch(this, new HttpCallback<List<VipBean>>() {
            @Override
            protected void onSuccess(List<VipBean> vipBeans) {
                adapter.setData(vipBeans);
            }
        });
        mBinding.setBean(UserCenter.userBean);
        if (UserCenter.isVip()) {
            mBinding.vipTitle.setText("到期时间：" + UserCenter.userBean.memberEndTime);
        }
        mBinding.avatar.setImageResource(AvatarUtil.getAvatar(UserCenter.userBean.gender));
    }

    @Override
    protected void initView() {
        super.initView();
        LinearLayoutManager layoutManager = (LinearLayoutManager) mBinding.recyclerView.getLayoutManager();
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        if (BuildConfig.DEBUG) {
            mBinding.agree.setChecked(true);
        }
    }

    VipBean current = null;
    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemVipBinding, VipBean> adapter = new BaseAdapter<ItemVipBinding, VipBean>() {
        @OnClick(R.id.root)
        public void root(VipBean bean) {
            current = bean;
            notifyDataSetChanged();
        }

        @Override
        protected void onBindData(ItemVipBinding binding, VipBean bean, int position) {

            super.onBindData(binding, bean, position);
            if (bean == current) {
                binding.root.setBackgroundResource(R.drawable.vip_bg3);
            } else {
                binding.root.setBackgroundResource(R.drawable.shape_fff4f6ff_round_12);
            }
            binding.old.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        }
    };

    public void go(View view) {
        if (current == null) {
            ToastUtils.showShort("请选择会员");
            return;
        }
        if (!mBinding.agree.isChecked()) {
            ToastUtils.showShort("请同意协议");
            return;
        }
        showPop();
//        API.scoreOrderCreate(current.id).watch(this, new HttpCallback<Double>() {
//        });
    }

    public static class PayInfo {
        public int resId;
        public String channel;
        public String name;

        public PayInfo(int resId, String channel, String name) {
            this.resId = resId;
            this.channel = channel;
            this.name = name;
        }
    }

    PayInfo payInfo = null;

    private void showPop() {
        BottomPopup bottomPopup = new BottomPopup<PopVipBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @Override
            protected void initData() {
                super.initData();
                mBinding.setBean(current);
            }

            @Override
            protected void initView() {
                super.initView();
                List<PayInfo> list = new ArrayList<>();
                list.add(new PayInfo(R.drawable.wechat_pay, "wechat", "微信支付"));
                list.add(new PayInfo(R.drawable.ali_pay, "ali", "支付宝支付"));
//                list.add(new PayInfo(R.drawable.appale, "apple", "Apple Pay"));
                payAdapter.setData(list);
            }

            @BindAdapter(R.id.recyclerView)
            BaseAdapter<ItemVipPayBinding, PayInfo> payAdapter = new BaseAdapter<ItemVipPayBinding, PayInfo>() {
                @Override
                protected void onBindData(ItemVipPayBinding binding, PayInfo bean, int position) {
                    super.onBindData(binding, bean, position);
                    if (bean == payInfo) {
                        binding.old.setChecked(true);
                    } else {
                        binding.old.setChecked(false);
                    }
                    binding.old.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if (isChecked) {
                                payInfo = bean;
                                notifyDataSetChanged();
                            }
                        }
                    });
                }
            };

            @OnClick(R.id.confirm)
            public void confirm() {
                if (payInfo == null) {
                    return;
                }
                pay();
                dismiss();
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    private void pay() {
        PayRequest payRequest = new PayRequest();
        payRequest.sku = current.sku;
        payRequest.channel = payInfo.channel;
        API.orderCreate(payRequest).watch(this, new HttpCallback<PayResponse>() {
            @Override
            protected void onSuccess(PayResponse response) {
                if (payInfo.channel.equals("wechat")) {

                } else {
                    AliPayUtils aliPayUtils = new AliPayUtils(mActivity);
                    aliPayUtils.setAliPayStatesCallBack(new AliPayUtils.AliPayStatesCallBack() {
                        @Override
                        public void onPaySuccess() {
                            Log.d(TAG, "onPaySuccess() called");
                        }

                        @Override
                        public void onPayFail() {
                            Log.d(TAG, "onPayFail() called");
                        }
                    });
                    aliPayUtils.pay(response.payInfo);
                }
            }
        });
    }
}